package com.hw.one.transpot.fileupload.util;

import com.hw.one.collect.config.ConditionConfig;
import com.hw.one.collect.util.DateUtil;
import com.hw.one.collect.util.ZipUtil;
import com.hw.one.transpot.constant.DeviceCodeConstant;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 * @Class UploadUtil
 * @Author guoshenzhen
 * @Date 2018/3/19  10:17
 */
public class UploadUtil {
    private static Logger log = Logger.getLogger(UploadUtil.class);
    // 保存临时文件的上级文件夹名称
    private static final String uploadTempDir = "uploadtemp";

    /**
     * 获取保存文件的路径
     * @return
     */
    static final String getSaveDir(){
        String fileSavePath = ConditionConfig.get("fileSavePath", ConditionConfig.fileUpload);
        if (null == fileSavePath || fileSavePath.trim().length() == 0) {
            File file = new File(UploadUtil.class.getClassLoader().getResource("").getFile());
            File[] fileArr = file.getParentFile().listFiles(new FilenameFilter(){
                @Override
                public boolean accept(File dir, String name) {
                    if(uploadTempDir.equals(name)){
                        return true;
                    }
                    return false;
                }});
            if(null == fileArr || fileArr.length == 0){
                File temp = new File(file.getParent() + File.separator + uploadTempDir);
                temp.mkdir();
                fileSavePath = temp.getPath();
            }else{
                fileSavePath = fileArr[0].getPath();
            }
        }
        File f = new File(fileSavePath);
        if(!f.exists()){
            f.mkdirs();
        }
        return fileSavePath;
    }

    /**
     *
     * 获取保存文件的 文件名全称
     * @return
     */
    public static  final String getSaveFileName(){
//        String fileSaveName = ConditionConfig.get("fileSaveName", ConditionConfig.fileUpload);
        String fileSaveName = getFileName();
        if(null == fileSaveName || fileSaveName.trim().length() == 0){
            fileSaveName = "temp.zip";
        }
        String fileName = getSaveDir() + File.separator + fileSaveName;
        return fileName;
    }


    private static final String getFileDir2(){
        String formartStr = DateUtil.parseDate(new Date(), "yyyyMMddHH");
        String fileName = formartStr + ".D";
        return fileName;
    }

    public static final String getFileName(){
        String fileName = String.format("%s_%s_%s.zip",
                DeviceCodeConstant.VOC_SITECODE,
                DeviceCodeConstant.vocDeviceCode,
                getFileDir2()
                );
//        return getFileDir2() + ".zip";
        return fileName;
    }

    public static final String getFileDir(){
        final String filePath = ConditionConfig.get("filePath", ConditionConfig.fileUpload);

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.HOUR_OF_DAY, -1);

        /**
         *  文件路径后缀，由于文件名是根据时间生成的，所以动态生成后缀
         */
        final String sufferFix1 = DateUtil.parseDate(new Date(), DateUtil.NO_SPLIT_SDF_Y) + File.separator +
                DateUtil.parseDate(new Date(), DateUtil.NO_SPLIT_SDF_YM) ;

        final String concreteDir = filePath + File.separator + sufferFix1 + File.separator + getFileDir2();
        return concreteDir;
    }

    /**
     * 压缩文件操作
     */
    public static final void compress(){
        File file = new File(getSaveFileName());
        if(file.exists()){
            file.delete();
        }
        String concreteDir = getFileDir();
        try {
            ZipUtil.toZip(concreteDir, file.getPath());
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    public static final void clearCompressFile(){
        File file = new File(getSaveFileName());
        if(file.exists()){
            file.delete();
        }
    }

}
